﻿package mortal.game.view.systemSetting.view
{
    import com.gengine.keyBoard.*;
    import com.mui.controls.*;
    import flash.events.*;
    import flash.utils.*;
    import mortal.common.global.*;
    import mortal.common.shortcutsKey.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.view.common.*;
    import mortal.mvc.core.*;

    public class ShortcutItemRenderer extends GCellRenderer
    {
        private var _nameTxt:GTextFiled;
        private var _btn:GButton;
        private var _inputTxt:GTextInput;
        private var _keyMapData:KeyMapData;
        private var _clickNum:Number = 0;
        private var _tempKeyCode:int = 0;
        private static var NoDefaultStr:String = "未设置";

        public function ShortcutItemRenderer()
        {
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this._nameTxt = UIFactory.textField("", 20, 4, 100, 29, this, GlobalStyle.textFormatPutong);
            this._btn = UIFactory.gButton("", 120, 4, 65, 22, this, "GroupButton");
            this._btn.configEventListener(MouseEvent.CLICK, this.onBtnClickHandler);
            this._btn.doubleClickEnabled = false;
            this._btn.focusEnabled = false;
            this._btn.mouseEnabled = true;
            this._btn.mouseChildren = false;
            this._inputTxt = UIFactory.gTextInput(120, 4, 65, 22, this);
            this.setInputVisible(false);
            this._inputTxt.textField.addEventListener(FocusEvent.FOCUS_OUT, this.onInputFocusOutHandler);
            this._inputTxt.textField.addEventListener(FocusEvent.FOCUS_IN, this.onInputfocusInHandler);
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            this._keyMapData = null;
            this.setInputVisible(false);
            this._nameTxt.dispose(param1);
            this._btn.dispose(param1);
            this._btn.doubleClickEnabled = true;
            this._inputTxt.dispose(param1);
            this._inputTxt.textField.removeEventListener(FocusEvent.FOCUS_OUT, this.onInputFocusOutHandler);
            this._inputTxt.textField.removeEventListener(FocusEvent.FOCUS_IN, this.onInputfocusInHandler);
            this._nameTxt = null;
            this._inputTxt = null;
            this._btn = null;
            return;
        }// end function

        override public function set data(param1:Object) : void
        {
            super.data = param1;
            this._keyMapData = param1 as KeyMapData;
            if (this._keyMapData)
            {
                this._nameTxt.text = this._keyMapData.keyMapName;
                if (this._keyMapData.keyData)
                {
                    this.setButtonLabel(this._keyMapData.tempName);
                }
                else
                {
                    this.setButtonLabel();
                }
                this._btn.enabled = this._keyMapData.isCanEdit;
                this._btn.drawNow();
            }
            else
            {
                this._nameTxt.text = "";
                this.setButtonLabel();
                this._keyMapData.tempName = "";
                this._btn.enabled = true;
            }
            return;
        }// end function

        private function onBtnClickHandler(event:MouseEvent) : void
        {
            if (this._clickNum < 1)
            {
                setTimeout(this.setShortcutData, 200);
            }
            var _loc_2:String = this;
            var _loc_3:* = this._clickNum + 1;
            _loc_2._clickNum = _loc_3;
            return;
        }// end function

        private function setShortcutData() : void
        {
            if (this._clickNum <= 1)
            {
                this.setInputVisible(true);
            }
            else
            {
                if (this._keyMapData.keyData)
                {
                    this._keyMapData.tempName = this._keyMapData.keyData.shortcutsName;
                }
                this.data = this._keyMapData;
            }
            this._clickNum = 0;
            return;
        }// end function

        private function onInputFocusOutHandler(event:FocusEvent) : void
        {
            this.setInputVisible(false);
            return;
        }// end function

        private function onInputfocusInHandler(event:FocusEvent) : void
        {
            KeyBoardManager.instance.changeImeEnable(false);
            return;
        }// end function

        private function setInputVisible(param1:Boolean) : void
        {
            this._inputTxt.visible = param1;
            this._btn.visible = !param1;
            if (param1)
            {
                this.stage.focus = this._inputTxt.textField;
                this._inputTxt.textField.text = this._btn.label;
                this.addEventListener(KeyboardEvent.KEY_DOWN, this.onKeyDownHandler);
                this.addEventListener(KeyboardEvent.KEY_UP, this.onKeyDownHandler);
                this._inputTxt.textField.addEventListener(TextEvent.TEXT_INPUT, this.onTextInputHandler);
            }
            else
            {
                if (this._inputTxt.text != "")
                {
                    this.setButtonLabel(this._inputTxt.text);
                }
                else
                {
                    this.setButtonLabel();
                }
                this.removeEventListener(KeyboardEvent.KEY_DOWN, this.onKeyDownHandler);
                this.removeEventListener(KeyboardEvent.KEY_UP, this.onKeyDownHandler);
                this._inputTxt.textField.removeEventListener(TextEvent.TEXT_INPUT, this.onTextInputHandler);
            }
            return;
        }// end function

        private function onTextInputHandler(event:TextEvent) : void
        {
            event.preventDefault();
            event.stopImmediatePropagation();
            return;
        }// end function

        private function onKeyDownHandler(event:KeyboardEvent) : void
        {
            var _loc_2:KeyMapData = null;
            if (event.type == KeyboardEvent.KEY_DOWN)
            {
                if (this._tempKeyCode == event.keyCode)
                {
                    return;
                }
                this._tempKeyCode = event.keyCode;
                if (event.keyCode == KeyCode.BackSpace)
                {
                    this.setInputTextTxt("");
                    if (this._keyMapData)
                    {
                        this._keyMapData.displayKeyData.keyCode = 0;
                        this._keyMapData.displayKeyData.isShift = false;
                    }
                }
                else if (ShortcutsKey.instance.hasKeyCode(event.keyCode))
                {
                    if (KeyBoardManager.ShiftKey)
                    {
                        this.setInputTextTxt("sh" + KeyCode.getKeyName(event.keyCode));
                        this.setInputVisible(false);
                    }
                    else
                    {
                        this.setInputTextTxt(KeyCode.getKeyName(event.keyCode));
                        this.setInputVisible(false);
                    }
                    _loc_2 = ShortcutsKey.instance.getKeyMapData(event.keyCode, KeyBoardManager.ShiftKey);
                    if (_loc_2 && _loc_2 != this._keyMapData)
                    {
                        _loc_2.tempName = NoDefaultStr;
                        _loc_2.displayKeyData.keyCode = 0;
                        _loc_2.displayKeyData.isShift = false;
                        Dispatcher.dispatchEvent(new DataEvent(EventName.ShortcutsUpdate, _loc_2));
                        MsgManager.showRollTipsMsg(_loc_2.keyMapName + "快捷键清除");
                    }
                    if (this._keyMapData)
                    {
                        this._keyMapData.displayKeyData.isShift = KeyBoardManager.ShiftKey;
                        this._keyMapData.displayKeyData.keyCode = event.keyCode;
                    }
                }
                else if (event.keyCode != KeyCode.SHIFT)
                {
                    MsgManager.showRollTipsMsg("不能使用的快捷键");
                }
            }
            else
            {
                this._tempKeyCode = 0;
            }
            return;
        }// end function

        private function setInputTextTxt(param1:String) : void
        {
            this._inputTxt.textField.text = param1;
            this._inputTxt.textField.setSelection(0, (param1.length - 1));
            return;
        }// end function

        private function setButtonLabel(param1:String = "") : void
        {
            if (param1 == null || param1 == "")
            {
                param1 = NoDefaultStr;
            }
            this._btn.label = param1;
            this._btn.drawNow();
            if (this._keyMapData)
            {
                this._keyMapData.tempName = param1;
            }
            return;
        }// end function

    }
}
